<?php
	error_reporting(0);
	session_start();
	error_reporting(1);

	global $site_root;
	
	require_once ($site_root . "common/PinSQL.obj");
	$pinSQL = new PinSQL();
	
	?>
	
	<div id="bulletincomments_">	
	
	<img src="/img/arrow_green.gif" />
	<a href="javascript:showBulletinPost()">Post a bulletin</a>
	
	<div id="bulletincommentdiv" style="display: none; "><blockquote>
			<span id="comment_status" style="display: none; font-size: 9pt;">Please wait...<img src="/img/rotating_arrow.gif" style="position: relative; top: 4px;"></span>

				<!--blockquote!-->
				<textarea name="comment" id="bulletincomment" rows=3 cols=29></textarea>
				<!--/blockquote!-->
				<input type="hidden" id="destuser" value="<?php echo $_SESSION['username']; ?>" >
			<br />
			<img src="/img/arrow_green.gif" /><a href="javascript:PostBulletin();"> Post bulletin</a>
		</blockquote></div>
	
	<p />
	


	<?php	
	
	$pinSQL->Query("SELECT * FROM bulletin ORDER BY date DESC");
	
	while ($row = $pinSQL->FetchNextRow())
	{
		$bulletin_id = $row['bulletin_id'];
		$user_id = $row['user_id'];
		$message = $row['message'];
		$date = $row['date'];
		$prettydate = date( 'M j', strtotime($date) );
     	$prettytime = date( 'g:ia', strtotime($date) );
		
		$thisUser = $pinSQL->GetUsername($user_id);
		?>
		
			<img src="/img/icons/bulletin_sm.png" /> <a href="/members/frame.php?page=profile&user=<?php echo $thisUser; ?>">
				<?php echo $pinSQL->GetFirstName($thisUser); ?>
			</a> wrote
			
			<br /><span class="subtle">
				on <?php echo $prettydate . " at " . $prettytime; ?>			
			</span>
			
			<blockquote>
				<?php echo $message; ?>			
			</blockquote>
			
			<div class="horizontal_separation"></div>
		
		<?php
	}
?>

	</div>
	
<script type="text/javascript">

function showBulletinPost()
{
	var postArea = document.getElementById('bulletincommentdiv');
	
	if (postArea.style.display == 'none')
	{
		postArea.style.display = 'block';
	}
	else
	{
		postArea.style.display = 'none';
	}
}

function PostBulletin()
{
	var postArea = document.getElementById('commentdiv');
	var username = document.getElementById('destuser').value;
	var message = document.getElementById('bulletincomment').value;
	
	var status = document.getElementById('comment_status');
	
	status.style.display = "block";
	
	 // create the XMLHttpRequest object
          var request;
          try { request = new XMLHttpRequest(); }
          catch(e)
          {
               try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
               catch(e)
               {
                    try { request = new ActiveXObject("Microsoft.XMLHTTP"); }
                    catch(e)
                    {
                         alert("Your browser will not support this webpage.");
                         return false;
                    }
               }
          }

          
          request.open("POST", 'post_bulletin.php', true);

          request.onreadystatechange = function()
          {
               if(request.readyState == 4)
               {
                    var responseText = request.responseText;
                    status.style.display = 'none';
                    postArea.style.display = 'none';
                 document.getElementById('bulletincomments_').innerHTML = responseText;
                 	document.getElementById('bulletincommentdiv').style.display = 'none';
                 	document.getElementById('bulletincomment').value = "";
               }
          }

		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		message = message.replace(/&/g, '%26');
		
		request.send('destuser=' + username + '&message=' + message);
	}


</script>
